<template>
  <div>
    <!--头部操作栏-->
    <el-form
      :inline="true"
      size="small">
      <el-form-item v-if="auth.add">
        <el-button
          icon="el-icon-plus"
          :disabled="loading"
          @click="handleCreate">新增</el-button>
      </el-form-item>

      <el-form-item>
        <el-button-group>
          <el-button
            v-if="auth.enable"
            icon="el-icon-check"
            :disabled="loading"
            @click="handleState(1)">启用</el-button>

          <el-button
            v-if="auth.disable"
            icon="el-icon-close"
            :disabled="loading"
            @click="handleState(0)">禁用</el-button>
        </el-button-group>
      </el-form-item>

      <el-form-item v-if="auth.del">
        <el-button
          icon="el-icon-delete"
          :disabled="loading"
          @click="handleDelete(multipleSelection)">删除</el-button>
      </el-form-item>
    </el-form>

    <!--数据列表-->
    <el-table
      v-loading="loading"
      :data="currentTableData"
      :highlight-current-row="true"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange">
      <el-table-column type="selection" width="50">
      </el-table-column>

      <el-table-column
        label="展厅名称"
        prop="showroomName"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{showroomTypeMap[scope.row.type]}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        prop="createTime"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="状态"
        prop="status"
        sortable="custom"
        align="center"
        width="100">
        <template slot-scope="scope">
          <el-tag
            size="mini"
            :type="statusMap[scope.row.status].type"
            style="cursor: pointer;"
            effect="light"
            @click.native="handleStatus(scope.$index)">
            {{statusMap[scope.row.status].text}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        min-width="140">
        <template slot-scope="scope">
          <el-button
            v-if="auth.set"
            size="small"
            @click="handleUpdate(scope.$index)"
            type="text">编辑</el-button>

          <el-button
            v-if="auth.del"
            size="small"
            @click="handleDelete(scope.$index)"
            type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--新增数据模态框-->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="600px">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px">
        <el-form-item
          label="展厅名称"
          prop="showroomName">
          <el-input
            v-model="form.showroomName"
            :disabled="dialogStatus !== 'create'"
            placeholder="请输入展厅名称"
            :clearable="true"/>
        </el-form-item>

        <el-form-item label-width="80px" label="上传图片">
          <el-upload
            class="upload-demo"
            action=""
            :limit=1
            :auto-upload=false
            :on-change="onchange"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="朝代" prop="dynasty">
          <el-select
            v-model="form.dynasty"
            placeholder="请选择"
            clearable>
            <el-option
              v-for="item in dynastyMap"
              :key="item.key"
              :label="item.text"
              :value="item.key"/>
          </el-select>
        </el-form-item>

        <el-form-item label="展厅类型">
          <el-select
            v-model="form.type"
            placeholder="请选择"
            clearable>
            <el-option
              v-for="item in showroomTypeArray"
              :key="item.key"
              :label="item.text"
              :value="item.key"/>
          </el-select>
        </el-form-item>

        <el-form-item
          label="排序索引">
          <el-input-number
            v-model="form.orderIndex"
            controls-position="right"
            placeholder="请输入排序索引值"
            :min="1" :max="50">
          </el-input-number>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          @click="dialogFormVisible = false"
          size="small">取消</el-button>

        <el-button
          v-if="dialogStatus === 'create'"
          type="primary"
          :loading="dialogLoading"
          @click="createShowroom"
          size="small">确定</el-button>

        <el-button
          v-else type="primary"
          :loading="dialogLoading"
          @click="update(form.index)"
          size="small">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import axios from 'axios'
import { addShowroom } from '@/api/showroom/showroom'
export default {
  components: {

  },
  props: {
    tableData: {
      default: () => []
    },
    loading: {
      default: false
    },
    group: {
      default: () => []
    }
  },
  data () {
    return {
      fileList: [],
      param: {},
      currentTableData: [],
      multipleSelection: [],
      auth: {
        add: true,
        del: true,
        set: true,
        enable: true,
        disable: true,
        reset: false
      },
      dialogLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dynastyMap: [
        { key: 1, text: '五大名窑' },
        { key: 2, text: '八大窑系' },
        { key: 3, text: '标本' }
      ],
      showroomTypeArray: [
        { key: 1, text: '收费展厅' },
        { key: 2, text: '免费展厅' }
      ],
      showroomTypeMap: {
        1: '收费展厅',
        2: '免费展厅'
      },
      statusMap: {
        1: {
          text: '开放',
          type: 'danger'
        },
        2: {
          text: '关闭',
          type: 'success'
        }
      },
      form: {
        showroomName: undefined,
        dynasty: undefined,
        type: undefined,
        orderIndex: 1,
        imgFile1: undefined
      },
      rules: {
        showroomName: [
          {
            required: true,
            message: '账号不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.currentTableData = val
      },
      immediate: true
    }
  },
  mounted () {
  },
  methods: {
    // 获取列表中的用户编号
    _getClientIdList (val) {
      let clients = []
      if (Array.isArray(val)) {
        val.forEach(value => {
          clients.push(value.admin_id)
        })
      } else {
        clients.push(this.currentTableData[val].admin_id)
      }

      return clients
    },
    // 选中数据项
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 获取排序字段
    sortChange ({ column, prop, order }) {
      let sort = {
        order_type: undefined,
        order_field: undefined
      }

      if (column && order) {
        sort.order_type = order === 'ascending' ? 'asc' : 'desc'
        sort.order_field = prop
      }

      this.$emit('sort', sort)
    },
    // 批量设置状态
    handleState (state) {
      let clients = this._getClientIdList(this.multipleSelection)
      if (clients.length === 0) {
        this.$message.error('请选择要操作的数据')
        return
      }

      this.$confirm('确定要执行该操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
        })
        .catch(() => {
        })
    },
    // 批量删除
    handleDelete (val) {
      let clients = this._getClientIdList(val)
      if (clients.length === 0) {
        this.$message.error('请选择要操作的数据')
        return
      }

      this.$confirm('确定要执行该操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
        })
        .catch(() => {
        })
    },
    // 弹出新建对话框
    handleCreate () {
      this.form = {
        username: undefined
      }

      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })

      this.dialogStatus = 'create'
      this.dialogLoading = false
      this.dialogFormVisible = true
    },
    // 弹出编辑对话框
    handleUpdate (index) {
      this.form = {
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })

      this.dialogStatus = 'update'
      this.dialogLoading = false
      this.dialogFormVisible = true
    },
    // 编辑
    update (index) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.dialogLoading = true
        }
      })
    },

    // 图片处理事件
    onchange (file, filesList) {
      this.param = new FormData()
      this.param.append('imgFile1', file.raw, file.raw.name)
      console.log('param:', this.param)
    },
    handleRemove (file, filesList) {
      this.param2.delete('file')
    },
    // 请求新建用户
    createShowroom () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.dialogLoading = true
          // 下面append的东西就会到form表单数据的fields中；
          this.param.append('showroomName', this.form.showroomName)
          this.param.append('dynasty', this.form.dynasty)
          this.param.append('type', this.form.type)
          this.param.append('orderIndex', this.form.orderIndex)
          addShowroom(this.param)
            .then(res => {
              this.$emit('refresh', true)
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    handleStatus (index) {
      console.log('index:', index)
    }
  }
}
</script>
